package ui.views.property;

import java.util.Calendar;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.PojoProperties;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.views.properties.tabbed.AbstractPropertySection;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ui.control.SearchTextComposite;
import ui.filter.StringArrayFilter;
import ui.tempdao.EgnRstScenarioDao;
import ui.util.CalendarUtil;
import ui.util.IStructuredViewer;
import dao.EgnRstScenarioId;
import de.ralfebert.rcputils.builder.table.ColumnBuilder;
import de.ralfebert.rcputils.builder.table.TableViewerBuilder;
import de.ralfebert.rcputils.builder.table.format.Formatter;
import de.ralfebert.rcputils.properties.BaseValue;

public class ScenarioRstSection extends AbstractPropertySection implements IStructuredViewer{
	private final static Logger logger = LoggerFactory.getLogger(ScenarioRstSection.class);
	private Text labelText;
	private StringArrayFilter filter;
	private EgnRstScenarioId sceId;
	private TableViewer tableViewer;
	private SearchTextComposite swtComp;

	public ScenarioRstSection(){
		this.filter = new StringArrayFilter();
		logger.debug("SceSection Start");
	}
	@Override
	public TableViewer getTableViewer(){
		return tableViewer;
	}
	@Override
	public TreeViewer getTreeViewer(){
		return null;
	}
	
	@Override
	public void setInput(IWorkbenchPart part, ISelection selection) {
//		logger.debug("Test Part:{},{}",part.toString(),part.getAdapter(IPropertySheetPage.class).getClass().toString());
//		TabbedPropertySheetPage propPage = (TabbedPropertySheetPage)part.getAdapter(IPropertySheetPage.class);
//		logger.debug("Test Part1:");
//		propPage.refresh();
//		logger.debug("Test Part2:");
//		super.setInput(part, selection);
		Object input = ((IStructuredSelection) selection).getFirstElement();
		
//		if( input instanceof EgnRstScenarioId){
			this.sceId = (EgnRstScenarioId) input;
			tableViewer.setInput(EgnRstScenarioDao.getEgnMarketVariScenario(sceId));
			Formatter.setZebraBackground(tableViewer);
//		}
	}

	@Override
	public void createControls(Composite parent, TabbedPropertySheetPage aTabbedPropertySheetPage) {
		super.createControls(parent, aTabbedPropertySheetPage);
		swtComp = new SearchTextComposite(parent,SWT.NONE,filter);
		Composite comp = swtComp.getComp();
		createTable(comp);
		bindValues();
//		Set Filter
		tableViewer.addFilter(filter);
		
	}
//	@Override
//	public void refresh() {
//	
//	}
	
	private void createTable(Composite parent){
		TableViewerBuilder t = new TableViewerBuilder(parent);

		ColumnBuilder baseDate = t.createColumn("BaseDate");
		// baseDate.bindToProperty("mvId");
		baseDate.bindToValue(new BaseValue<Object[]>() {
			@Override
			public Object get(Object[] mvHis) {
//				logger.debug("in the innerClass");
				return CalendarUtil.toString((Calendar) mvHis[0]);
			}
			
		});
		// baseDate.setPercentWidth(60);
		baseDate.useAsDefaultSortColumn();
		baseDate.build();

		ColumnBuilder ircId = t.createColumn("MV Id");
		ircId.bindToValue(new BaseValue<Object[]>() {
			@Override
			public Object get(Object[] mvHis) {
				return mvHis[1];
			}
		});
		ircId.build();

		ColumnBuilder maturity = t.createColumn("Maturity");
		maturity.bindToValue(new BaseValue<Object[]>() {
			@Override
			public Object get(Object[] mvHis) {
				return mvHis[2];
			}
		});
		maturity.build();

		ColumnBuilder intRate = t.createColumn("MV Value");
		intRate.bindToValue(new BaseValue<Object[]>() {
			@Override
			public Object get(Object[] mvHis) {
				return mvHis[3];
			}
		});
		intRate.build();

		ColumnBuilder sceValue = t.createColumn("Sce Value");
		sceValue.bindToValue(new BaseValue<Object[]>() {
			@Override
			public Object get(Object[] mvHis) {
				return (Double) mvHis[4]
						* Math.pow((Double) mvHis[3], (Double) mvHis[5])
						+ (Double) mvHis[6];
			}
		});
		sceValue.build();

		ColumnBuilder coefficient = t.createColumn("Coefficient");
		coefficient.bindToValue(new BaseValue<Object[]>() {
			@Override
			public Object get(Object[] mvHis) {
				return mvHis[4];
			}
		});
		coefficient.build();

		ColumnBuilder power = t.createColumn("Power");
		power.bindToValue(new BaseValue<Object[]>() {
			@Override
			public Object get(Object[] mvHis) {
				return mvHis[5];
			}
		});
		power.build();

		ColumnBuilder spread = t.createColumn("Spread");
		spread.bindToValue(new BaseValue<Object[]>() {
			@Override
			public Object get(Object[] mvHis) {
				return mvHis[6];
			}
		});
//		spread.setPercentWidth(60);
		spread.build();

		t.setInput(null);
		tableViewer = t.getTableViewer();		
	}
	
	private void bindValues() {
		DataBindingContext bindingContext = new DataBindingContext();
		
		IObservableValue myModel = PojoProperties.value(EgnRstScenarioId.class,
				"sceId").observe(sceId);
		IObservableValue myWidget = WidgetProperties.text(SWT.Modify).observe(
				swtComp.getSearchText());
		bindingContext.bindValue(myWidget, myModel);
	}
}
